﻿Imports System.Drawing.Drawing2D
Imports OpenHardwareMonitor.Hardware
Imports GPU_Info_Extractor

Public Class frmTaskBar

#Region " Declerations "

	Dim GPU_Info_ As New GPU_Info_Extractor.GPUMonitor_Extractor
	Dim temp As Double = 0

	Dim stopMonitor As Boolean = False

	Dim fontToUse As Font = New Font("Tahoma", 3, FontStyle.Regular, GraphicsUnit.Millimeter)

	Dim brushToUse As Brush = New SolidBrush(My.Settings.color1)

	Dim bitmapToDraw As Bitmap = New Bitmap(16, 16)

	Dim drawIcon As Graphics = Drawing.Graphics.FromImage(bitmapToDraw)

	Dim hIcon As IntPtr

	Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Int32) As Int32

	Dim linearBrush As New LinearGradientBrush(Me.DisplayRectangle, Color.Blue, Color.Black, Drawing2D.LinearGradientMode.BackwardDiagonal)

#End Region

#Region " Functions "

	Private Sub task_Report()
		getGPUinfo()
		setIconColor()
		createTextIcon()
	End Sub

	Sub createTextIcon()

		Try

			drawIcon.Clear(Color.Transparent)
			drawIcon.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

			drawIcon.DrawString(temp.ToString, fontToUse, brushToUse, 0, 1)
			hIcon = (bitmapToDraw.GetHicon)

			If Not hIcon = 0 Then
				notifyIcon.Icon = Drawing.Icon.FromHandle(hIcon)

			End If


		Catch exc As Exception

			MessageBox.Show(exc.InnerException.ToString, "  Create Text Icon", _
			 MessageBoxButtons.OK, MessageBoxIcon.Error)
			End
		End Try

	End Sub

	Private Sub setIconColor()
		Try
			If temp >= My.Settings.criticalMin Then
				brushToUse = New SolidBrush(My.Settings.color4)
			ElseIf temp >= My.Settings.hotMin AndAlso temp <= My.Settings.hotMax Then
				brushToUse = New SolidBrush(My.Settings.color3)
			ElseIf temp >= My.Settings.acceptableMin AndAlso temp <= My.Settings.acceptableMax Then
				brushToUse = New SolidBrush(My.Settings.color2)
			ElseIf temp <= My.Settings.coolMax Then
				brushToUse = New SolidBrush(My.Settings.color1)
			Else
				brushToUse = New SolidBrush(Color.White)
			End If
		Catch ex As Exception
			MsgBox("error" & " " & ex.Message)
			End
		End Try

	End Sub

	Private Sub getGPUinfo()

		Try
			temp = GPU_Info_.GetGPUInfo_NotifyIcon(SensorType.Temperature)
			My.Application.DoEvents()

		Catch ex As Exception
			MessageBox.Show(ex.InnerException.ToString, "Somethings not right?", _
			 MessageBoxButtons.OK, MessageBoxIcon.Error)
			End
		End Try


	End Sub

#End Region

#Region " Form "

	Private Sub frmTaskBar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Try

			Me.Hide()
			Me.Visible = False
			Me.Icon = My.Resources.Monitor_Started
			sysMonTray.Icon = Me.Icon
			sysMonTray.ShowBalloonTip(500, "Aurora GPU Monitor", "GPU Monitor Started", ToolTipIcon.Info)
		Catch exc As Exception

			MessageBox.Show(exc.InnerException.ToString, "Somethings not right?", _
			 MessageBoxButtons.OK, MessageBoxIcon.Error)

		End Try

	End Sub

	Private Sub frmTaskBar_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

		Try

			notifyIcon.Icon = Nothing
			notifyIcon.Dispose()
			sysMonTray.Icon = Nothing
			sysMonTray.Dispose()

		Catch ex As Exception

		End Try
	End Sub

#End Region

#Region " GUI "

	Private Sub sysMonTray_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sysMonTray.MouseDoubleClick
		Try
			CMSMainfrm.PerformClick()
		Catch ex As Exception

		End Try
	End Sub

	Private Sub notifyIcon_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles notifyIcon.MouseMove
		Try

			notifyIcon.Text = "Fan - " & " " & GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU", SensorType.Fan) & " " & " RPM" & " " & " | " & " " & "Fan Control" & " " & GPU_Info_.getFromOpenHardwareMonitor_SensorValue_Graphs("GPU Fan", SensorType.Control) & " " & " % "
		Catch ex As Exception

		End Try
	End Sub

	Private Sub CMS_Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMS_Start.Click
		Try
			sysMonTray.Icon = My.Resources.Monitor_Started
			TimrTask.Enabled = True

		Catch ex As Exception
			MessageBox.Show(ex.InnerException.ToString, "Somethings not right?", _
			MessageBoxButtons.OK, MessageBoxIcon.Error)

		End Try

	End Sub

	Private Sub CMS_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMS_Stop.Click
		Try
			sysMonTray.Icon = My.Resources.Monitor_Stopped
			TimrTask.Enabled = False
			notifyIcon.Icon = Nothing

		Catch ex As Exception

			MessageBox.Show(ex.InnerException.ToString, "Somethings not right?", _
			 MessageBoxButtons.OK, MessageBoxIcon.Error)

		End Try
	End Sub

	Private Sub CMSExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMSExit.Click
		Try
			notifyIcon.Icon = Nothing
			notifyIcon.Dispose()
			sysMonTray.Icon = Nothing
			sysMonTray.Dispose()

			End
		Catch ex As Exception

		End Try
	End Sub

	Private Sub CMSMainfrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMSMainfrm.Click
		Try
			frmMain.Show()
			notifyIcon.Icon = Nothing
			notifyIcon.Dispose()
			sysMonTray.Icon = Nothing
			sysMonTray.Dispose()
			Me.Close()

		Catch ex As Exception

		End Try
	End Sub

	Private Sub CMSSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMSSettings.Click
		Try
			frmSettings.Show()
		Catch ex As Exception

		End Try
	End Sub

#End Region

#Region " Timers "

	Private Sub TimrTask_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimrTask.Tick
		Try
			task_Report()
		Catch ex As Exception

		End Try
	End Sub

#End Region

	
End Class